package com.copyctrl.publisher.send;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * @author:JH
 * @version:1.0 用于发送处理数据的消息
 */
@Slf4j
@Component
public class DataPublisher {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void redisData(String msg) {
        log.info("往redis消息队列发送数据:" + msg);
        rabbitTemplate.convertAndSend("direct.data.exchange", "redis", msg);
    }

    public void mysqlData(String msg) {
        log.info("往mysql消息队列发送数据:" + msg);
        rabbitTemplate.convertAndSend("direct.data.exchange", "mysql", msg);
    }

    public void deleteFile(String fileName) {
        log.info("待删除的图片:" + fileName);
        rabbitTemplate.convertAndSend("direct.data.exchange", "file", fileName,new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //延迟三十分钟 测试先设置五分钟
                message.getMessageProperties().setHeader("x-delay", 3000000);
                return message;
            }
        });
    }
}

